<?php
$transactionLog = array();
$labelData[] = 'Date';
foreach ($accounts as $key => $a) {
    $labelData[] = str_replace('PNC - ', '', $a['Account']['name']);

    ksort($a['Balances']);
    $thisAccountData = null;
    foreach ($a['Balances'] as $date => $data) {
        //$chartData['Datas'][$key][$data['date']] = $data['total'];
        $chartData['Datas'][$key][$data['date']] = $data['total'];
    }
    foreach ($a['History'] as $date => $transactions) {
        if (!empty($transactions)) {
            $transData = array(
                'Account' => $a['Account'],
                'History' => $transactions,
            );
            $transactionLog[$date][] = $transData;
        }
    }
}
$outputTable[] = $labelData;

// debug($chartData);
foreach ($chartData['Datas'][0] as $date => $data) {
    $rowData = null;
    $rowData[] = $date;
    for ($i = 0; $i < count($chartData['Datas']); $i++) {
        $rowData[] = $chartData['Datas'][$i][$date];
    }
    $outputTable[] = $rowData;
}
//debug($outputTable);
//debug(json_encode($outputTable));

function array_reverse_order($array){
        $array_key = array_keys($array);
        $array_value = array_values($array);
        
        $array_return = array();
        for($i=1, $size_of_array=sizeof($array_key);$i<=$size_of_array;$i++){
            $array_return[$array_key[$size_of_array-$i]] = $array_value[$size_of_array-$i];
        }
        
        return $array_return;
    }


?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = google.visualization.arrayToDataTable(<?php echo json_encode($outputTable); ?>);

        var options = {
            title: 'Account Overview'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    }
</script>
<div class="accounts view">
    <?php
    foreach ($accountList as $id => $name) {
        echo $this->Html->link($name, 'review/' . $id) . ' | ';
    }
    echo $this->Html->link('All', 'review/') . '  ';
    ?>
    <div id="chart_div" style="width: 940px; height: 500px;"></div>
    <hr>
    <h2><?php __('Recent Transactions'); ?></h2>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <?php if (true) : ?> <th>Account</th><?php endif; ?>
            <th>Date</th>
            <th>Description</th>
            <th>Charge</th>
            <th class="actions"><?php __('Actions'); ?></th>
        </tr>
        <?php 
        $i = 0;
        
        $today = new DateTime();
        $todayTs = $today->getTimestamp();
        
        
        foreach (array_reverse_order($transactionLog) as $date => $datesTranactionData):        
            foreach ($datesTranactionData as $a):
            foreach ($a['History'] as $t):
                $class = '';
                if ($i++ % 2 == 0) {
                    $class .= ' altrow';
                }
                
                $class .= ($t['Transaction']['verified'] != 1) ? ' unverified' : ' verified';
                
                $postDate = new DateTime($t['Transaction']['post_date']);               
                if ($postDate->getTimestamp() >= $todayTs){
                    $class = ' future';
                }
                ?>
                <tr class="<?php echo $class; ?>"">
                    <?php if (true) : ?> <td><?php echo $a['Account']['name'];?>&nbsp;</td><?php endif; ?>
                    <td><?php echo $t['Transaction']['post_date'];?></td>
                    <td><?php echo $t['Transaction']['name'];?></td>
                    <td style="text-align: right;"><?php echo number_format($t['Transaction']['amount'], 2, '.', '');?></td>
                    <td class="actions">
                        <?php echo $this->Html->link(__('View', true), array('action' => 'view', $a['Account']['id'])); ?>
                        <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $a['Account']['id'])); ?>
                        <?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $a['Account']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $a['Account']['id'])); ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php endforeach; ?>
        <?php endforeach; ?>
    </table>
</div>